#include "tempiritornomodel.h"
#include "QtDebug.h"

TempiRitornoModel::TempiRitornoModel(TempiRitornoL* ptr_tr_list, QObject *parent)
    : QAbstractTableModel(parent)
{
    m_tr_list = ptr_tr_list;
}

int TempiRitornoModel::rowCount(const QModelIndex &parent) const
{
    return(m_tr_list->sizeOf());
}

int TempiRitornoModel::columnCount(const QModelIndex &parent) const
{
        return 9;
}

QVariant TempiRitornoModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid())
		 return QVariant();

	if (index.row() < 0)
		 return QVariant();

    if (index.row() >= m_tr_list->sizeOf())
		 return QVariant();

	if (index.column() < 0)
		 return QVariant();

        if (index.column() >= 9)
		 return QVariant();

	TempiRitorno tr;
    tr = m_tr_list->getAt(index.row());
	if (role == Qt::DisplayRole)
	{
		QString qstr;
		switch (index.column())
        {
            case 0:
                return QVariant(qstr.fromStdString(tr.getKeyStr()));
            case 1:
				return QVariant(qstr.fromStdString(tr.tp_cu.getFlagStr()));
            case 2:
                if (tr.isCustom())
                    return QVariant("---");
                else
                    return QVariant(tr.tp_cu.getFlagValue());
            case 3:
                    return QVariant(qstr.fromStdString(tr.tp_co.getFlagStr()));
            case 4:
                if (tr.isCustom())
                    return QVariant("---");
                else
                    return QVariant(tr.tp_co.getFlagValue());
            case 5:
                return QVariant(qstr.fromStdString(tr.tp_sls.getFlagStr()));
            case 6:
                if (tr.isCustom())
                    return QVariant("---");
                else
                    return QVariant(tr.tp_sls.getFlagValue());
            case 7:
                qstr.setNum(tr.getFlagValue(),'f',3);
                return QVariant(qstr);
            case 8:
                return QVariant(qstr.fromStdString(tr.getDescr()));
		}
		return QVariant();
	}
	else if (role == Qt::UserRole)
	{
		return QVariant::fromValue(tr);
	}
        if (index.column() >= 0 && index.column() <=8 && role == Qt::TextAlignmentRole)
		return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
	else return QVariant();
}

QVariant TempiRitornoModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	 if (role != Qt::DisplayRole)
		 return QVariant();

	 if (orientation == Qt::Horizontal)
		switch (section)
		{
			case 0:
                return tr("ID");
            case 1:
				return tr("Classe d'uso");
            case 2:
				return tr("Cu");
            case 3:
				return tr("Tipo costruzione");
            case 4:
				return tr("Vn");
            case 5:
                return tr("S.L. sismico");
            case 6:
				return tr("Pvr");
            case 7:
                return tr("Tr");
            case 8:
                return tr("Nome Stato Limite");
		}
	 else
         return QString("S.L. n.%1").arg(section+1);
}


bool TempiRitornoModel::insertRows(int position, int rows, const QModelIndex &parent)
{
	Q_UNUSED(parent);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    m_tr_list->insertAt(position, rows);
	endInsertRows();
	return true;
}

bool TempiRitornoModel::removeRows(int position, int rows, const QModelIndex &parent)
{
	 Q_UNUSED(parent);
	 beginRemoveRows(QModelIndex(), position, position+rows-1);
	 for (int row=0; row < rows; ++row) {
         m_tr_list->remove(position);
	 }
	 endRemoveRows();
	 return true;
}

bool TempiRitornoModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (index.isValid() && role == Qt::EditRole){
		int row = index.row();
        TempiRitorno tr = value.value < TempiRitorno >();
        m_tr_list->rename(row,tr.getKeyStr(),tr.getDescr());
        m_tr_list->valueAt(row) = tr;
        QModelIndex i = this->index(row,8);
        emit( dataChanged( index, i ) );
	}
	else return false;
	return true;
}

void TempiRitornoModel::updateWEDChange(void)
{
    reset();
    emit( dataChanged(QModelIndex(),QModelIndex()) );
}

